package com.mla.fastdfs.controller;


import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.github.tobato.fastdfs.service.AppendFileStorageClient;
import com.mla.fastdfs.commom.Result;
import com.mla.fastdfs.commom.UpLoadConstant;
import com.mla.fastdfs.dto.RoleDto;
import com.mla.fastdfs.dto.UploadUpInfoVO;
import com.mla.fastdfs.pojo.Group;
import com.mla.fastdfs.pojo.UploadUpInfo;
import com.mla.fastdfs.pojo.User;
import com.mla.fastdfs.service.IUploadUpInfoService;
import com.mla.fastdfs.service.UserService;
import com.mla.fastdfs.utils.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 上传明细 前端控制器
 * </p>
 *
 * @author astupidcoder
 * @since 2021-03-25
 */
@RestController
@RequestMapping("/upload-up-info")
public class UploadUpInfoController {

    @Autowired
    private IUploadUpInfoService uploadUpInfoService;
    @Autowired
    private UserService userService;
    @Autowired
    private AppendFileStorageClient appendFileStorageClient;

    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public Result del(Integer id) {
        Assert.notNull(id, "id不能为空");
        UploadUpInfo info = uploadUpInfoService.getById(id);
        if (info == null) {
            return Result.error("文件不存在");
        }
        String url = info.getFileUrl();
        url = url.replace(UpLoadConstant.VISIT_URL, "");

        appendFileStorageClient.deleteFile(UpLoadConstant.DEFAULT_GROUP, url);
        uploadUpInfoService.removeById(id);
        return Result.ok();
    }

    @RequestMapping(value = "/selectByPrimaryKey", method = RequestMethod.POST)
    public Result selectByPrimaryKey(Integer id) {
        Assert.notNull(id, "id不能为空");
        UploadUpInfo info = uploadUpInfoService.getById(id);
        return Result.ok(info);
    }

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public Result getList(UploadUpInfo record) {
        /**条件判断**/
        List<UploadUpInfoVO> list = uploadUpInfoService.getList(record);
        return Result.ok(list);
    }

    @RequestMapping(value = "/pageList", method = RequestMethod.POST)
    public Result pageList(UploadUpInfo record, String aToken, String userId,
                           @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                           @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize) {
        /**条件判断**/
        if (StringUtil.isNotEmpty(aToken)) {
            User user = userService.getUser(aToken);
            if (user == null) {
                return Result.error("未登录");
            }
            userId = user.getId();
        }
        RoleDto dto = userService.getByRole(userId);
        if (dto != null && !dto.getCode().equals("administration")) {
            record.setUid(userId);
        }

        return uploadUpInfoService.pageList(record, pageNo, pageSize);
    }

    @PostMapping("/getByIdentifier")
    public Result getByIdentifier(String identifier, String uid, Integer groupId) {
        Assert.notNull(identifier, "上传唯一标识不能为空");
        if (groupId == null) {
            groupId = 1;
        }
        if (StringUtil.isEmpty(uid)) {
            uid = "1";
        }
        return uploadUpInfoService.getByIdentifier(identifier, groupId, uid);
    }
}
